package Ptolemaiosz;

import java.util.ArrayList;
import java.awt.Polygon;
import java.awt.geom.Rectangle2D;

public class CustomPolygon {
    
	ArrayList<Vector> vertexes = new ArrayList<Vector>();

	void addVertex(Vector v)
    {
        vertexes.add(v);
    }
	
	ArrayList<Vector> getVertexes() {
		return vertexes;
	}

	static boolean isCollided(Vector v1, CustomPolygon p1, Vector v2, CustomPolygon p2) {
		ArrayList<Vector> vertex1 = p1.getVertexes();				//lekerdezzuk a csucspontokat
		ArrayList<Vector> vertex2 = p2.getVertexes();
		
                Polygon poly1 = new Polygon();								
		Polygon poly2 = new Polygon();								//jawa.awt polygonok keszitese a csucspontokbol								
		
                function(vertex1, v1, poly1);
		function(vertex2, v2, poly2);
		
                Rectangle2D bounds = poly1.getBounds2D();					//bounding box lekerdezese
		boolean result = poly2.intersects(bounds);					//utkoztetes az elobb lekert bounding box segitsegevel
		
                //Degug
                //System.err.println("Boundingbox1: XMax=" + poly1.getBounds2D().getMaxX() + " XMin=" + poly1.getBounds2D().getMinX() + " YMax=" + poly1.getBounds2D().getMaxY() + " YMin=" + poly1.getBounds2D().getMinY());
                //System.err.println("Boundingbox2: XMax=" + poly2.getBounds2D().getMaxX() + " XMin=" + poly2.getBounds2D().getMinX() + " YMax=" + poly2.getBounds2D().getMaxY() + " YMin=" + poly2.getBounds2D().getMinY());                
                return result;	
	}
	
	static void function (ArrayList<Vector> vertex, Vector v, Polygon poly) {
		for (int i = 0; i < vertex.size(); i++) {
			Vector temp = new Vector(0.0f, 0.0f);
			temp.add(v);											
			temp.add(vertex.get(i));								//csucspont koord.-k meghatarozasa a poziciobol es csucspontvektorokbol
			temp.multiply(100000.0f);								//azert kell, mert a java-s polygon csak integereket kezel
			poly.addPoint((int)temp.getX(), (int)temp.getY());		//csucspontok hozzaadasa az uj poligonhoz
		}
	}
}